var organizationId = sessionStorage.getItem("id")
/*首页获取登录信息并且渲染首页顶部*/
function headHtml(){
	var uid = $("#useridoflogin").val();
	var usertype = $("#usertypeoflogin").val();
	var token = $("#token").val();
	var headHtml = "";
	if(uid != 'null' && uid != ''){
		userInfo(uid,usertype,token);
	}else if(organizationId != null && organizationId != ""){
		getOrganizationUser(organizationId);
	}else{
		headHtml += '<ul>\
			<li class="fl" style="margin-right: 20px"><a href="newLogin">机构用户登录</a></li>\
		<li class="fl" style="margin-right: 20px"><a href="http://xmsoc.com:20507/OCEAN/toLogin?url='+indexUrl+'">登录</a></li>\
		<li class="fl"><a href="http://xmsoc.com:20507/OCEAN/register/goRegister">注册</a></li>\
		</ul>';
		$(".username").html(headHtml);
	}
}



/*获取登录状态*/
function userInfo(uid,usertype,token){
	$.ajax({
		url:Server + '/thirdparty/user/userInfo',
		type:'POST',
		data:{
			'uid':uid,
			'type':usertype,
			'token':token
		},
		dataType : "json",
		success:function(data){
			if(data.code == 0 && data.userInfo != null){
				console.log("登录状态有效");
				getUser(uid,usertype,token);
			}else{
				alert("登录状态已失效,请重新登录");
				$(location).attr("href","http://xmsoc.com:20507/OCEAN/api/login?url="+indexUrl+"")
			}
			
		},
		error:function(data){}
	});
}

/*获取登录用户的信息*/
function getUser(uid,usertype,token){
	$.ajax({
		url:Server + "/thirdparty/user/getUserInfoById",
		type:"POST",
		data:{
			"uid":uid,
			"token":token,
			"type":usertype
		},
		dataType:"json",
		success:function(data){
			console.log(JSON.stringify(data));
			var headHtml = "";
			if(data.code == '0'){
			var userUrl = "";
			var userCenter = "new_file3";
			if(data.userType == 2){
				userCenter = "yiqixinxiweihu15";
			}
			var userInfo = data["userInfo"];
			if(userInfo != "" && userInfo != null){
				headHtml += '<ul>\
                    <li id="username" onclick="btnLinkclick()">\
                <a onclick="userImg();">'+userInfo['account']+'</a>\
                <img id="userImg" src="images/icon1.png">\
            </li>\
        </ul>\
        <ul  class="dropdown-nav" id="dropdown-nav" style="display: none">\
            <!--<img src="images/icon9.png">-->\
            <li>\
                <a class="active" href="'+userCenter+'">用户中心</a>\
            </li>\
            <li>\
                <a href="outlogin">退出登录</a>\
            </li>\
        </ul>';
			}
			}else if(data.code == '1'){
				alert(data.msg);
				headHtml += '<ul>\
					<li class="fl" style="margin-right: 20px"><a href="newLogin">机构用户登录</a></li>\
					<li class="fl" style="margin-right: 20px"><a href="http://xmsoc.com:20507/OCEAN/toLogin?url='+indexUrl+'">登录</a></li>\
					<li class="fl"><a href="http://xmsoc.com:20507/OCEAN/register/goRegister">注册</a></li>\
					</ul>';
			}
			$(".username").html(headHtml);
		},
		error:function(data){
			console.log("失败");
		}
	});
}


function getOrganizationUser(organizationId){
	$.ajax({
		url:Server + "/userCenter/head",
		type:"POST",
		data:{
			'id':organizationId,
		},
		success:function(data){
			console.log(data);
			if(data != "" && data != null){
				headHtml = '<ul>\
                    <li id="username" onclick="btnLinkclick()">\
                <a onclick="userImg();">'+data+'</a>\
                <img id="userImg" src="images/icon1.png">\
            </li>\
        </ul>\
        <ul  class="dropdown-nav" id="dropdown-nav" style="display: none">\
            <!--<img src="images/icon9.png">-->\
            <li>\
                <a href="ins28" class="active">机构用户</a>\
            </li>\
            <li>\
                <a onclick="outLogin()">退出登录</a>\
            </li>\
        </ul>';
			}else{
				headHtml = '<ul>\
					<li class="fl" style="margin-right: 20px"><a href="newLogin">机构用户登录</a></li>\
					<li class="fl" style="margin-right: 20px"><a href="http://xmsoc.com:20507/OCEAN/toLogin?url='+indexUrl+'">登录</a></li>\
					<li class="fl"><a href="http://xmsoc.com:20507/OCEAN/register/goRegister">注册</a></li>\
					</ul>';
			}
			$(".username").html(headHtml);
		},
		error:function(data){
			console.log("失败");
		}
	});
}


function btnLinkclick(){
    if($("#dropdown-nav").css("display")=="none"){
        $("#dropdown-nav").show();
    }else{
        $("#dropdown-nav").hide();
    }

}

function Show_Hidden(obj) {
    if (obj.style.display == "block") {
        obj.style.display = 'none';
    } else {
        obj.style.display = 'block';
    }
}
window.onload = function() {
    var olink = document.getElementById("username");
    var odiv = document.getElementById("dropdown-nav");
    olink.onclick = function() {
        Show_Hidden(odiv);
        return false;
    }
}
//退出登录
function outLogin(){
	sessionStorage.clear();
	window.location.href="outlogin"
	
}
// nodsbcidsabvnon

